package top.zhangjianyong.blog.config.fastjson;

import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.support.config.FastJsonConfig;
import com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter;
import java.util.List;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * fastjson 参数序列化器，仅作用于 web 层，保证 Spring AI 相关 HTTP 客户端依然使用 jackson
 *
 * @author zhangjianyong
 * @since 2024/7/19 下午2:17
 */
@Configuration
public class FastJsonMessageConfig implements WebMvcConfigurer {
  @Override
  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
    fastJsonConfig.setWriterFeatures(JSONWriter.Feature.BrowserCompatible);
    converter.setFastJsonConfig(fastJsonConfig);
    // 只在 web 层添加 fastjson，不移除 jackson，保证 AI 相关 HTTP 客户端依然用 jackson
    converters.add(0, converter);
  }
}
